package xyz.zdsoft.personal_info_madule.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.zdsoft.common.utils.DateUtils;
import xyz.zdsoft.common.utils.RandomUtils;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.common.utils.ResultBody;
import xyz.zdsoft.personal_info_madule.service.StudentInfoService;
import xyz.zdsoft.personal_info_madule.vo.ClassHourOfTeamVo;
import xyz.zdsoft.personal_info_madule.vo.ProjectScoreVo;
import xyz.zdsoft.personal_info_madule.vo.StuInfoVo;
import xyz.zdsoft.personal_info_madule.vo.TeamRank;

import java.io.File;
import java.io.IOException;

/**
 * @author 夏兵
 */
@RestController
@Api(tags = "学生个人信息管理")
@RequestMapping("/api/student")
public class StudentInfoController {

    @Value("${logoFile}")
    private String logoFile;

    @Autowired
    private StudentInfoService studentInfoService;

    @GetMapping("/queryStuInfo")
    @ApiOperation("查询学生的个人基本信息")
    public Result<StuInfoVo> queryStuInfo() {
        return studentInfoService.queryStuInfo();
    }

    @PostMapping("/changeFace")
    @ApiOperation("换头像(当前用户)")
    public ResultBody changeFace(@RequestPart MultipartFile file) {
        if (file == null) {
            return ResultBody.error("上传头像不能为空");
        }
        String logoName = file.getOriginalFilename();
        if (StringUtils.isBlank(logoName)) {
            return ResultBody.error("文件名不能为空");
        }
        new File(logoFile).mkdirs();
        String type = file.getContentType().toUpperCase();
        if (!(type.contains("BMP") || type.contains("PNG")
                || type.contains("JPEG") || type.contains("GIF"))) {
            return ResultBody.error("上传头像必须是BMP、PNG、JPEG、GIF格式");
        }
        int size = 1024 * 1024 * 2;
        if (file.getSize() > size) {
            return ResultBody.error("上传头像大小不大于2MB");
        }
        logoName = DateUtils.dateTimeNow() + RandomUtils.getSixBitRandom() + logoName.substring(logoName.lastIndexOf("."));
        String newPath = logoFile + logoName;
        try {
            file.transferTo(new File(newPath));
        } catch (IOException e) {
            return ResultBody.error("文件上传失败");
        }
        return studentInfoService.fileDelAndUpdate(logoName);
    }

    @GetMapping("/changeEmail")
    @ApiOperation("修改邮箱")
    @ApiImplicitParam(name = "email", value = "邮箱", required = true)
    public ResultBody changeEmail(String email) {
        return studentInfoService.changeEmail(email);
    }

    @GetMapping("/changePhone")
    @ApiOperation("修改电话号码")
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true)
    public ResultBody changePhone(String phone) {
        return studentInfoService.changePhone(phone);
    }

    @GetMapping("/queryTeamClassHour")
    @ApiOperation("查看实训小组成员信息")
    public Result<ClassHourOfTeamVo> queryTeamClassHour() {
        return studentInfoService.queryTeamClassHour();
    }

    @GetMapping("/queryTeamScoreForCou")
    @ApiOperation("查看项目得分")
    public Result<ProjectScoreVo> queryTeamScoreForCou() {
        return studentInfoService.queryTeamScoreForCou();
    }

    @GetMapping("/queryTeamRank")
    @ApiOperation("查看该课程的所有小组排名和得分")
    @ApiImplicitParam(name = "couId", value = "课程id", required = true)
    public Result<TeamRank> queryTeamRank(Integer couId) {
        return studentInfoService.queryTeamRank(couId);
    }
}
